package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 十九、BECK抑郁问卷（Bdi）共13题
 */
public class Table19 extends AbstractTable implements TableCalculate{
    String code = "Bdi";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetBdiFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return null;
    }

    private static ClsAnswerResult GetBdiFactorScore(ClsAnswerRequest answer) {
        // 总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 13;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i;
            // 共一组，0,1,2,3

            int vFactor = 0; // 实际为1个因子

            // 总分
            for (i = 0; i < sAnswer.length; i++) {
                vFactor += Integer.valueOf(sAnswer[i]) - 1;
            }

            // 按“总分”得到“诊断结果”
            // 按“总分”得到“指导意见”
            int[] intValueA = { 4, 13, 21 };
            String[] strValueA = { "无抑郁或极轻度", "轻度", "中度", "重度" };
            String vAppraise = strValueA[strValueA.length - 1];
            String vDoctorIdea = "Bdi.1.4";
            for (i = 0; i < intValueA.length; i++) {
                if (vFactor <= intValueA[i]) {
                    vAppraise = strValueA[i];
                    vDoctorIdea = "Bdi.1." + String.valueOf(i + 1);
                    break;
                }
            }

            ClsScore.Bdi bdi = new ClsScore.Bdi();

            bdi.setTotal_o(vFactor);
            bdi.setTotal_a(vAppraise);
            bdi.setTotal_d(vDoctorIdea);

            result.setObject(bdi);
            return result;

        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


}
